//http://www.jankoatwarpspeed.com/post/2009/09/28/webform-wizard-jquery.aspx
(function ($) {
	$.fn.formToWizard = function (options) {
		options = $.extend({
			submitButton: "",
			startStep: 0,
			btnType: '',
			startText: 'Next >',
			nextText: 'Next >',
			prevText: '< Back',
			startImg: '',
			nextImg: '',
			prevImg: '',
			stepName: 'Step',
			stepClass: '',
			stepsWidth: '',
			stepsClass: '',
			funcCallBack: null,
			stepCallBack: null,
			stepValidate: null,
			valGroup: ''
		}, options);

		var element = this;

		if ($(element).prev().attr('id') === 'steps') return;

		var steps = $(element).find("fieldset");
		var count = steps.size();
		var submmitButtonName = options.submitButton;
		var stepsUL;
		var stepsLI = new Array();
		var stepsDiv = new Array();
		var stepsCmd = new Array();
		
		if (count > 1) {
			$(submmitButtonName).hide();

			// 2
			if (options.stepsClass.length > 0)
				$(element).before("<ul id='steps' class='C5SLLC_steps " + options.stepsClass + "'></ul>");
			else
				$(element).before("<ul id='steps' class='C5SLLC_steps'></ul>");

			stepsUL = $(element).prev();

			steps.each(function (i) {
				$(this).wrap("<div id='step" + i + "'></div>");
				stepsDiv.push($(this).parent());

				$(this).append("<p id='step" + i + "commands'></p>");
				stepsCmd.push($(this).children().last());


				// 2
				var name = $(this).find("legend").html();
				if (options.stepName.length > 0)
					stepsUL.append("<li style='list-style:none !important;' id='stepDesc" + i + "' class='" + options.stepClass + "'>" + options.stepName + " " + (i + 1) + "<span>" + name + "</span></li>");
				else
					stepsUL.append("<li style='list-style:none !important;' id='stepDesc" + i + "' class='" + options.stepClass + "'><span>" + name + "</span></li>");

				stepsLI.push(stepsUL.children().last());

				if (i == 0) {
					createNextButton(i);
					selectStep(i);
				}
				else if (i == count - 1) {
					stepsDiv[i].hide();
					createPrevButton(i);
					moveDoneButton(i);
				}
				else {
					stepsDiv[i].hide();
					createPrevButton(i);
					createNextButton(i);
				}
			});

			var width = 0;
			stepsUL.find('li').each(function () {
				width += $(this).outerWidth() + 2;
			});

			if (options.stepsWidth.indexOf('%') < 0) {
				if (options.stepsWidth > 0)
					if (width > options.stepsWidth)
						width = options.stepsWidth;
				stepsUL.css("width", width + 'px');
			}
			else
				stepsUL.css("max-width", width + 'px');

			if (options.startStep > 0) {
				stepsDiv[0].hide();
				stepsDiv[options.startStep].show();
				selectStep(options.startStep);
				if (options.startStep == count - 1)
					$(submmitButtonName).show();
			}

			if (typeof options.funcCallBack == "function") options.funcCallBack();
			return this;
		}

		function moveDoneButton(i) {
			//var stepName = "step" + i;
			$(submmitButtonName).appendTo(stepsCmd[i]).addClass('wzddone');
		}

		function createPrevButton(i) {
			var button;
			var stepName = "step" + i;

			switch (options.btnType) {
				case "ImageButton":
					button = $('<a href="#" id="' + stepName + 'Prev" class="CommandButton prev"><img src="' + options.prevImg + '" alt="' + options.prevText + '" /></a>');
					break;
				case "LinkButton":
					button = $("<a href='#' id='" + stepName + "Prev' class='CommandButton prev'>" + options.prevText + "</a>");
					break;
				default:
					button = $('<input type="button" id="' + stepName + 'Prev" class="CommandButton prev" value="' + options.prevText + '" />');
					break;
			}

			stepsCmd[i].append(button);

			button.bind("click", function (e) {
				stepsDiv[i].hide();
				stepsDiv[i - 1].show();
				$(submmitButtonName).hide();
				selectStep(i - 1);
				if (typeof options.stepCallBack == "function") options.stepCallBack('prev');
			});
		}

		function createNextButton(i) {
			var button;
			var buttonText = options.nextText;
			var buttonImage = options.nextImg;
			var stepName = "step" + i;
			if (i == 0) {
				buttonText = options.startText;
				buttonImage = options.startImg;
			}

			switch (options.btnType) {
				case "ImageButton":
					button = $('<a href="#" id="' + stepName + 'Next" class="CommandButton next"><img src="' + buttonImage + '" alt="' + options.buttonText + '" /></a>');
					break;
				case "LinkButton":
					button = $("<a href='#' id='" + stepName + "Next' class='CommandButton next'>" + buttonText + "</a>");
					break;
				default:
					button = $('<input type="button" id="' + stepName + 'Next" class="CommandButton next" value="' + buttonText + '" />');
					break;
			}

			stepsCmd[i].append(button);

			button.bind("click", function (e) {
				e.preventDefault();
				if (typeof options.stepValidate == "function")
					if (!options.stepValidate(stepName, options.valGroup))
						return;
				stepsDiv[i].hide();
				stepsDiv[i + 1].show();
				if (i + 2 == count)
					$(submmitButtonName).show();
				selectStep(i + 1);
				if (typeof options.stepCallBack == "function") options.stepCallBack('next');
			});
		}

		function selectStep(i) {
			$.each(stepsLI, function() { $(this).removeClass("current") });
			stepsLI[i].addClass("current");
		}

	}
})(jQuery);